package com.ficus.road.maintain.service.smartnew;

import com.ficus.road.maintain.core.model.RoadTypeEnum;
import com.ficus.road.maintain.core.model.smart.HighwayEngineering;
import com.ficus.road.maintain.core.model.smart.HighwayWarehouse;
import com.ficus.road.maintain.core.model.smartnew.SmartRoadSection;
import com.ficus.road.maintain.dao.smartnew.SmartRoadSectionDao;
import lombok.RequiredArgsConstructor;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author xiawen yang
 * @date 2022/3/1 下午7:10
 */
@Service
@RequiredArgsConstructor
public class SmartRoadSectionService {
    private final SmartRoadSectionDao smartRoadSectionDao;

    public List<SmartRoadSection> findHighwaySection() {
        Query query = new Query(Criteria.where("road_type").is(RoadTypeEnum.HIGHWAY.toString()));
        List<SmartRoadSection> roadSections = smartRoadSectionDao.findByQuery(query);
        return roadSections;
    }

    public List<SmartRoadSection> findRoadSection() {
        Query query = new Query(Criteria.where("road_type").is(RoadTypeEnum.ROAD.toString()));
        List<SmartRoadSection> roadSections = smartRoadSectionDao.findByQuery(query);
        return roadSections;
    }

    public List<SmartRoadSection> findAllSection() {
        return smartRoadSectionDao.findAll();
    }

    public void deleteById(String id) {
        smartRoadSectionDao.deleteById(id);
    }

    public List<SmartRoadSection> findByQuery(Query query) {
        return smartRoadSectionDao.findByQuery(query);
    }

    public SmartRoadSection findById(String id) {
        return smartRoadSectionDao.fetch(id);
    }

    public HighwayEngineering convert2Engineering(SmartRoadSection smartRoadSection) {
        return HighwayEngineering.builder()
                .id(smartRoadSection.getId())
                .road(smartRoadSection.getRoadName())
                .roadType(smartRoadSection.getRoadCharacter())
                .roadLevel(smartRoadSection.getLevel())
                .rqi(smartRoadSection.getRQI() + "")
                .pssi(smartRoadSection.getPSSI() + "")
                .area(Double.parseDouble(smartRoadSection.getArea()))
                .rdi(smartRoadSection.getRDI())
                .pci(smartRoadSection.getPCI())
                .build();
    }

    public HighwayWarehouse convert2HighwayWarehouse(SmartRoadSection smartRoadSection) {
        return HighwayWarehouse.builder()
                .id(smartRoadSection.getId())
                .road(smartRoadSection.getRoadName())
                .roadType(smartRoadSection.getRoadCharacter())
                .roadLevel(smartRoadSection.getLevel())
                .pci(smartRoadSection.getPCI())
                .rqi(smartRoadSection.getRQI())
                .rdi(smartRoadSection.getRDI())
                .pssi(smartRoadSection.getPSSI())
                .area(Integer.parseInt(smartRoadSection.getArea()))
                .build();
    }


}
